package com.example.administrator.xxnapp.login;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.annotation.TargetApi;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.support.annotation.NonNull;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.app.LoaderManager.LoaderCallbacks;

import android.content.CursorLoader;
import android.content.Loader;
import android.database.Cursor;
import android.net.Uri;
import android.os.AsyncTask;

import android.os.Build;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.text.TextUtils;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.EditorInfo;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import com.example.administrator.xxnapp.AppPresenter;
import com.example.administrator.xxnapp.BaseActivity;
import com.example.administrator.xxnapp.R;
import com.example.administrator.xxnapp.bean.UserBean;
import com.example.administrator.xxnapp.http.ResultStringCallback;
import com.example.administrator.xxnapp.main.MainActivity;
import com.example.administrator.xxnapp.util.Config;
import com.example.administrator.xxnapp.util.des3.Des3;
import com.google.gson.Gson;
import com.lzy.okgo.OkGo;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnCheckedChanged;
import butterknife.OnClick;
import cn.mdruby.baselibrary.ui.EditTextDrawableClick;
import cn.mdruby.baselibrary.utils.ObjectStoreUtil;

import static android.Manifest.permission.READ_CONTACTS;

/**
 *
 */
public class LoginActivity extends BaseActivity implements IViewUser,IViewVersion{
    @BindView(R.id.act_login_ET_Phone)
    EditTextDrawableClick mETPhone;
    @BindView(R.id.act_login_ET_Password)
    EditTextDrawableClick mETPassword;
    @BindView(R.id.act_login_Btn_Sign_In)
    Button mBtnSignIn;
    @BindView(R.id.act_login_CB_Remember)
    CheckBox mCBRemember;

    private UserPresenter mPresenter;
    private boolean lookPassword = false;
    private SharedPreferences preferences;

    @Override
    public void initView() {
        ButterKnife.bind(this);
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_login;
    }

    @Override
    public void initBeforeDatas() {
        mImmersionBar.statusBarColor(R.color.white).init();
        mPresenter = new UserPresenter(this,new UserBean());
        preferences = getSharedPreferences(Config.ShareKey.SHARED_PREFRENCES_NAME,MODE_PRIVATE);
    }

    @Override
    public void setListeners() {
        mETPassword.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView textView, int id, KeyEvent keyEvent) {
                if (id == EditorInfo.IME_ACTION_DONE || id == EditorInfo.IME_NULL) {
                    signIn(mBtnSignIn);
                    return true;
                }
                return false;
            }
        });
        mETPassword.setDrawableRightListener(new EditTextDrawableClick.DrawableRightListener() {
            @Override
            public void onDrawableRightClick(View view) {
                lookPassword = !lookPassword;
                setDrawableETPassword(lookPassword);
            }
        });

        mCBRemember.setChecked(preferences.getBoolean(Config.ShareKey.REMEMBER_PASSWORD_KEY,false));

        mCBRemember.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                SharedPreferences.Editor edit = preferences.edit();
                edit.putBoolean(Config.ShareKey.REMEMBER_PASSWORD_KEY,b);
                edit.commit();
            }
        });

//        mETPhone.setText("18515573396");
//        mETPassword.setText("WZ1234anghen");
    }

    private void setDrawableETPassword(boolean look){
        mETPassword.setDrawableRight(look?getResources().getDrawable(R.mipmap.login_password_look_icon):getResources().getDrawable(R.mipmap.login_password_unlook_icon));
        mETPassword.setTransformationMethod(look? HideReturnsTransformationMethod.getInstance(): PasswordTransformationMethod.getInstance());
        if (mETPassword.getText().toString().length()>0){
            mETPassword.setSelection(mETPassword.getText().toString().length());
        }
    }

    @OnClick(R.id.act_login_Btn_Sign_In)
    public void signIn(View view){
        String userPhone = mETPhone.getText().toString();
        String password = mETPassword.getText().toString();
        if (TextUtils.isEmpty(userPhone)){
            Toast.makeText(mContext, "请输入用户名", Toast.LENGTH_SHORT).show();
        }else if (TextUtils.isEmpty(password)){
            Toast.makeText(mContext, "请输入密码", Toast.LENGTH_SHORT).show();
        }else {
            mPresenter.signIn(userPhone,password);
        }
    }

    @OnClick(R.id.act_login_Btn_Sign_Up)
    public void signUp(View view){
        Intent intent = new Intent(mContext,RegisterActivity.class);
        startActivity(intent);
    }

    @OnClick(R.id.act_login_TV_Forget_Password)
    public void forgetPassword(View view){
        Intent intent = new Intent(mContext,ForgetPasswordActivity.class);
        startActivity(intent);
    }

    @Override
    public void loginSuccess(UserBean bean) {
        SharedPreferences.Editor edit = preferences.edit();
        edit.putString(Config.ShareKey.USER_PASSWORD,mETPassword.getText().toString());
        edit.commit();
        ObjectStoreUtil.saveObject(mContext, Config.Key.USER_OBJECT_KEY,bean);
        Intent intent = new Intent(mContext, MainActivity.class);
        startActivity(intent);
        finish();
    }


    @Override
    public void loginError(String message) {
        Toast.makeText(mContext, message, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onGetVersionSuccess(Intent intent) {

    }
}

